package com.onlineMIS.ORM.entity.headQ.finance;


import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.onlineMIS.ORM.entity.chainS.report.ChainSalesReport;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Color;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Product;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrderProduct;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.ExcelTemplate;
import com.onlineMIS.common.loggerLocal;

import antlr.StringUtils;
import freemarker.template.utility.StringUtil;

//financeBillTask
public class FinanceBillReportTemplate extends ExcelTemplate{
	
	protected Map<Integer, String> typeHQMap = new LinkedHashMap<Integer, String>();
	protected Map<Integer, String> typeChainMap = new LinkedHashMap<Integer, String>();
	protected Map<Integer, String> status = new LinkedHashMap<Integer, String>();
	
	private static final long serialVersionUID = 1578267557986495659L;{
		typeHQMap.put(FINANCE_INCOME_HQ, "收款单");
		typeHQMap.put(FINANCE_PAID_HQ, "付款单");
		typeHQMap.put(FINANCE_PREINCOME_HQ, "预收款单");
		typeHQMap.put(FINANCE_PREINCOME_HQ_R, "预收款退单");
		typeHQMap.put(FINANCE_DECREASE_HQ, "应收减少单据");
		typeHQMap.put(FINANCE_INCREASE_HQ, "应收增加单据");
		
		typeChainMap.put(FINANCE_INCOME_HQ, "付款单");
		typeChainMap.put(FINANCE_PAID_HQ, "收款单");
		typeChainMap.put(FINANCE_PREINCOME_HQ, "预付款单");
		typeChainMap.put(FINANCE_PREINCOME_HQ_R, "预付款退单");
		typeChainMap.put(FINANCE_DECREASE_HQ, "应付减少单据");
		typeChainMap.put(FINANCE_INCREASE_HQ, "应付增加单据");
		
		status.put(-1, "全部");
		status.put(1, "草稿");
		status.put(2, "完成");
		status.put(3, "红冲");
	}
	
	public static final int FINANCE_PAID_HQ = 1;
	public static final int FINANCE_INCOME_HQ = 2;
	public static final int FINANCE_DECREASE_HQ = 3;
	public static final int FINANCE_INCREASE_HQ = 4;
	public static final int FINANCE_PREINCOME_HQ = 5;
	public static final int FINANCE_PREINCOME_HQ_R = 6;
	
	private final int date_row_ind = 1;
	private final int data_row_ind = 5;
	
	private final int chain_column = 0;
	private final int sales_q_column= 1;

	
	private List<FinanceBillVO> rptEles = new ArrayList<FinanceBillVO>();
	private Date startDate;
	private Date endDate;
//	private String path;
	private String chainName;

	public FinanceBillReportTemplate(Date startDate, Date endDate,String chainName, String templateWorkbookPath, List<FinanceBillVO> rptEles) throws IOException{
		super(templateWorkbookPath);		
		this.startDate = startDate;
		this.endDate = endDate;
		this.chainName = chainName;
		this.rptEles = rptEles;

	}
	
	public HSSFWorkbook process(){
		HSSFSheet sheet = templateWorkbook.getSheetAt(0);
        
		//写报表头信息
		String startDate = Common_util.dateFormat.format(this.startDate);
		String endDate = Common_util.dateFormat.format(this.endDate);

		
		Row headerRow2 = sheet.getRow(1);
		loggerLocal.info(Integer.toString(this.chainName.length()));
		if (this.chainName.equals("")) {
			this.chainName = "所有连锁店";
		}
		
		loggerLocal.info("连锁店："+this.chainName);
		
		headerRow2.createCell(1).setCellValue(this.chainName);
		
		Row headerRow3 = sheet.getRow(2);
		headerRow3.createCell(1).setCellValue(startDate);
		
		Row headerRow4 = sheet.getRow(3);
		headerRow4.createCell(1).setCellValue(endDate);
		
		//
		
		for (int i = 0; i < rptEles.size(); i++){
			FinanceBillVO saleReport = rptEles.get(i);
			Row row = sheet.createRow(i + data_row_ind);
			row.createCell(chain_column).setCellValue(saleReport.getChainName());
			row.createCell(sales_q_column).setCellValue(saleReport.getBillId());
			row.createCell(2).setCellValue(typeHQMap.get(saleReport.getBillType())); // 单据种类
			row.createCell(3).setCellValue(saleReport.getDate().toString()); // 单据日期
			row.createCell(4).setCellValue(saleReport.getAmount()); // 金额
			row.createCell(5).setCellValue(status.get(saleReport.getStatus())); // 状态
			row.createCell(6).setCellValue(saleReport.getRemark());
		}
		return templateWorkbook;
	}

}
